// url: 'https://w.wallhaven.cc/full/d6/wallhaven-d6jv7m.jpg'
let tableData = [
  {
    id: 1,
    name: '王美丽',
    age: 19,
    sex: '女',
    address: '杭州市',
    banji: '3班',
    shengri: '2025-01-01',
    url: 'https://w.wallhaven.cc/full/d6/wallhaven-d6jv7m.jpg'
  },
  {
    id: 2,
    name: '张三',
    age: 18,
    sex: '男',
    address: '开鲁县',
    banji: '2班',
    shengri: '2025-05-01',
    url: 'https://w.wallhaven.cc/full/d6/wallhaven-d6jv7m.jpg'
  },
  {
    id: 3,
    name: '李晓琳',
    age: 20,
    sex: '女',
    address: '广州市',
    banji: '5班',
    shengri: '2025-05-02',
    url: 'https://w.wallhaven.cc/full/d6/wallhaven-d6jv7m.jpg'
  },
  {
    id: 4,
    name: '王芳芳',
    age: 24,
    sex: '女',
    address: '北京市',
    banji: '1班',
    shengri: '2025-05-03',
    url: 'https://w.wallhaven.cc/full/d6/wallhaven-d6jv7m.jpg'
  },
  {
    id: 5,
    name: '张晓娟',
    age: 18,
    sex: '女',
    address: '上海市',
    banji: '4班',
    shengri: '2025-05-04',
    url: 'https://w.wallhaven.cc/full/d6/wallhaven-d6jv7m.jpg'
  },
  {
    id: 6,
    name: '李婷婷',
    age: 21,
    sex: '女',
    address: '深圳市',
    banji: '6班',
    shengri: '2025-05-05',
    url: 'https://w.wallhaven.cc/full/d6/wallhaven-d6jv7m.jpg'
  },
  {
    id: 7,
    name: '王雪梅',
    age: 23,
    sex: '女',
    address: '重庆市',
    banji: '2班',
    shengri: '2025-05-06',
    url: 'https://w.wallhaven.cc/full/d6/wallhaven-d6jv7m.jpg'
  },
  {
    id: 8,
    name: '张丽丽',
    age: 19,
    sex: '女',
    address: '武汉市',
    banji: '3班',
    shengri: '2025-05-10',
    url: 'https://w.wallhaven.cc/full/d6/wallhaven-d6jv7m.jpg'
  },
  {
    id: 9,
    name: '李娜娜',
    age: 25,
    sex: '女',
    address: '西安市',
    banji: '5班',
    shengri: '2025-05-07',
    url: 'https://w.wallhaven.cc/full/d6/wallhaven-d6jv7m.jpg'
  },
  {
    id: 10,
    name: '王小芳',
    age: 20,
    sex: '女',
    address: '南京市',
    banji: '1班',
    shengri: '2025-05-08',
    url: 'https://w.wallhaven.cc/full/d6/wallhaven-d6jv7m.jpg'
  },
  {
    id: 11,
    name: '张晓芳',
    age: 22,
    sex: '女',
    address: '大连市',
    banji: '4班',
    shengri: '2025-05-09',
    url: 'https://w.wallhaven.cc/full/d6/wallhaven-d6jv7m.jpg'
  },
  {
    id: 12,
    name: '李晓梅',
    age: 18,
    sex: '女',
    address: '青岛市',
    banji: '6班',
    shengri: '2025-05-09',
    url: 'https://w.wallhaven.cc/full/d6/wallhaven-d6jv7m.jpg'
  },
  {
    id: 13,
    name: '王小娟',
    age: 23,
    sex: '女',
    address: '沈阳市',
    banji: '2班',
    shengri: '2025-05-09',
    url: 'https://w.wallhaven.cc/full/d6/wallhaven-d6jv7m.jpg'
  },
  {
    id: 14,
    name: '张晓婷',
    age: 24,
    sex: '女',
    address: '长沙市',
    banji: '3班',
    shengri: '2025-05-10',
    url: 'https://w.wallhaven.cc/full/d6/wallhaven-d6jv7m.jpg'
  }
]

export async function paginate({ pageNum = 1, pageSize = 10, ...params }, data = tableData) {
  await new Promise(resolve => setTimeout(resolve, 2000))
  data = data.filter(item => {
    for (const key in params) {
      if (params[key] && !String((item as any)[key]).includes(String(params[key]))) {
        return false
      }
    }
    return true
  })
  const total = data.length
  const totalPages = Math.ceil(total / pageSize)
  pageNum = Math.max(pageNum, 1)
  pageNum = Math.min(pageNum, totalPages)
  const startIndex = (pageNum - 1) * pageSize
  const endIndex = pageNum * pageSize
  const currentPageData = data.slice(startIndex, endIndex)
  return {
    data: currentPageData,
    pagination: {
      pageNum,
      pageSize,
      total
    }
  }
}

export async function del(id: number) {
  await new Promise(resolve => setTimeout(resolve, 2000))
  tableData = tableData.filter(item => item.id !== id)
}

export async function changeSex(data: any) {
  await new Promise(resolve => setTimeout(resolve, 2000))
  const idx = tableData.findIndex(item => item.id === data.id)
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
  const [_, key] = Object.keys(data)
  ;(tableData[idx] as any)[key] = data[key]
}
